//
//  LoginCell.swift
//  PhotoLine
//
//  Created by 吴帅 on 17/9/14.
//  Copyright © 2017年 PhotoLine. All rights reserved.
//

import UIKit

class LoginCell: BaseTableViewCell, UITextFieldDelegate {
    
    public static let cellHeight : CGFloat = 75;
    
    public lazy var textField: UITextField! = {
        () -> UITextField in
        let textField = UITextField.init();
        textField.font = UIFont.font(15);
        return textField;
    }()
    private lazy var placeholderLabel: UILabel = {
        () -> UILabel in
        let label = UILabel.init();
        label.font = UIFont.font(13);
        return label;
    }()
    private let line: CALayer = {
        () -> CALayer in
        let layer = CALayer.init();
        layer.backgroundColor = UIColor(red:0.91, green:0.91, blue:0.91, alpha:0.5).cgColor;
        layer.frame = CGRect.init(x: 30, y: cellHeight-1, width: SCREEN_WIDTH-100, height: 1.5);
        return layer;
    }()
    
    // MARK: - LiftCycle
    
    override func layoutSubviews() {
        super.layoutSubviews();
        
        // 添加line
        self.layer.addSublayer(line);
        self.contentView.addSubview(self.textField);
        self.contentView.addSubview(self.placeholderLabel);
        self.textField.delegate = self;

        self.textField.snp.makeConstraints { (make) in
            make.bottom.equalTo(self.contentView).offset(-5);
            make.left.equalTo(self.contentView).offset(30);
            make.right.equalTo(self.contentView).offset(-30);
            make.height.equalTo(30);
        }
        self.placeholderLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self.textField).offset(5);
            make.centerY.equalTo(self.textField);
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }

    
    // MARK: - UITextFieldDelegate
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        self.setupPlaceholderFrame(y: -30);
        return true;
    }
    
    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
        if (textField.text?.isEmpty)! {
            self.setupPlaceholderFrame(y: 0);
        }
        return true;
    }
    
    // MARK: - private
    func setupPlaceholderFrame(y: Float) {
        self.placeholderLabel.snp.updateConstraints({ (make) in
            make.centerY.equalTo(self.textField).offset(y);
        });
        UIView.animate(withDuration: 0.2) {
            self.layoutIfNeeded();
        };
    }
    // MARK: - public
    
    open override class func cellWithTableView(tableView: UITableView, identifier: String) -> UITableViewCell {
        let cell = super.cellWithTableView(tableView: tableView, identifier: identifier);
        cell.backgroundColor = UIColor.white;
        cell.selectionStyle = UITableViewCellSelectionStyle.none;
        
        return cell;
    }
    
    /// 设置textField placeholder
    ///
    /// - parameter placeholder: placeholder
    public func setPlaceholder(placeholder:String) {
        
        let attrs = [NSForegroundColorAttributeName:UIColor(red:0.91, green:0.91, blue:0.91, alpha:1)];
        let attStr = NSAttributedString.init(string: placeholder, attributes: attrs);
        placeholderLabel.attributedText = attStr;
    }
}
